using UnityEngine;
using System.Collections;

public class PatrolBehaviour : MonoBehaviour
{
    public GameObject startWaypooint;
    public GameObject endWaypoint;
    public float movementSpeed = 2.0f;

    private GameObject m_currentWaypoint;
    private Vector3 m_direction;
    private SpriteAnimationEnemy spriteAnimation;
    public void Awake()
    {

        this.spriteAnimation = GetComponentInChildren<SpriteAnimationEnemy>();
    }
    public GameObject currentWaypoint
    {
        set
        {
            this.m_currentWaypoint = value;
        }
    }

    public Vector3 direction
    {
        get
        {
            return this.m_direction;
        }
    }

    // Use this for initialization
    void Start()
    {


        this.m_currentWaypoint = this.startWaypooint;

        this.GetDirection();
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(this.m_direction * Time.deltaTime * this.movementSpeed);
    }

    // switch to the next waypoint and calc the new direction
    public void SwitchWaypoint()
    {
        this.m_currentWaypoint = (this.m_currentWaypoint == this.startWaypooint)
            ? this.endWaypoint
            : this.startWaypooint;
        if (this.m_currentWaypoint == this.endWaypoint)
        {
            this.spriteAnimation.PlayWalkRightAnimation();
        }
        else
        {
            this.spriteAnimation.PlayWalkLeftAnimation();
        }
        this.GetDirection();
    }

    // do some simple direction calc
    private void GetDirection()
    {
        this.m_direction = new Vector3(this.m_currentWaypoint.transform.position.x - this.transform.position.x, 0, 0).normalized;
    }
}
